﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using XXX.Models;
using XXX.Models.XXXEntities;
using SML_XieTong;
using fuzhu;
using XXX.Common;

namespace XXX.Bo
{
    /// <summary>
    /// 实现BOM取数，主从表，嵌套
    /// </summary>
    public class MockInventoryRepository : IInventoryRepository
    {


        public RetModel Add(LK_Inventory inventory)
        {
            RetModel retModel = new RetModel();
            try
            {

                if (string.IsNullOrEmpty(inventory.InvCode))
                {
                    throw new Exception("货号不能为空");
                }
                if (string.IsNullOrEmpty(inventory.InvAddCode))
                {
                    throw new Exception("CAS号不能为空");
                }
                if (string.IsNullOrEmpty(inventory.InvName))
                {
                    throw new Exception("中文名不能为空");
                }
                if (inventory.InvEnglishName.Length>200)
                {
                    throw new Exception("英文名长度不能超多200");
                }

                //  特殊字符的替换
                inventory.InvName = inventory.InvName.Replace("'", "’");
                inventory.InvEnglishName = inventory.InvEnglishName.Replace("'", "’");
                 

                ////检查系统是否存在
                //string sql = string.Format("select count(*) from inventory where cinvcode =  '{0}' ", inventory.InvCode);
                //int iCnt = DbHelper.GetDbInt(DbHelper2.ExecuteScalar(sql, DbHelper.conStr));
                //if (iCnt > 0)
                //{
                //    retModel.code = 500;
                //    retModel.message = "货号已存在！";
                //}
                //else
                //{
                //    //删除附属表的内容
                //    sql = string.Format("delete  from inventory_sub where cinvsubcode = '{0}' ", inventory.InvCode);
                //  DbHelper2.ExecuteNonQuery(sql, DbHelper.conStr);
                //    sql = string.Format("delete  from bas_part where invcode =  '{0}' ", inventory.InvCode);
                //    DbHelper2.ExecuteNonQuery(sql, DbHelper.conStr);
                //    sql = string.Format("delete  from inventory_extradefine where cinvcode = '{0}' ", inventory.InvCode);
                //    DbHelper2.ExecuteNonQuery(sql, DbHelper.conStr);

                    U8API u8API = new U8API();
                   String cRe =  u8API.InsertInventory(inventory, CanShu.conStr);
                    if (cRe=="ok")
                    {
                        retModel.code = 200;
                        retModel.message = "存货增加成功！";
                    }
                    else
                    {
                        retModel.code = 500;
                        retModel.message = cRe;
                    }
                //}
                    
               
               
            }
            catch (Exception ex)
            {
                retModel.code = 500;
                retModel.message = ex.Message;


            }
            return retModel;


        }

        public RetModel AddList(List<LK_Inventory> inventory)
        {

            RetModel retModel = new RetModel();
            List<InventoryResult> lst = new List<InventoryResult>();

            int ierror = 0;
               for(int i=0;i<inventory.Count;i++)
                {

                    try
                    {
                        if (string.IsNullOrEmpty(inventory[i].InvCode))
                        {
                            throw new Exception("货号不能为空");
                        }
                        if (string.IsNullOrEmpty(inventory[i].InvAddCode))
                        {
                            throw new Exception("CAS号不能为空");
                        }
                        if (string.IsNullOrEmpty(inventory[i].InvName))
                        {
                            throw new Exception("中文名不能为空");
                        }
                        if (inventory[i].InvEnglishName.Length > 200)
                        {
                            throw new Exception("英文名长度不能超多200");
                        }

                        //  特殊字符的替换
                        inventory[i].InvName = inventory[i].InvName.Replace("'", "’");
                        inventory[i].InvEnglishName = inventory[i].InvEnglishName.Replace("'", "’");


                        ////检查系统是否存在
                        //string sql = string.Format("select count(*) from inventory where cinvcode =  '{0}' ", inventory[i].InvCode);
                        //int iCnt = DbHelper.GetDbInt(DbHelper2.ExecuteScalar(sql, DbHelper.conStr));
                        //if (iCnt > 0)
                        //{
                        //    throw new Exception("货号已存在");
                        //}
                        //else
                        //{
                            ////删除附属表的内容
                            //sql = string.Format("delete  from inventory_sub where cinvsubcode = '{0}' ", inventory[i].InvCode);
                            //DbHelper2.ExecuteNonQuery(sql, DbHelper.conStr);
                            //sql = string.Format("delete  from bas_part where invcode =  '{0}' ", inventory[i].InvCode);
                            //DbHelper2.ExecuteNonQuery(sql, DbHelper.conStr);
                            //sql = string.Format("delete  from inventory_extradefine where cinvcode = '{0}' ", inventory[i].InvCode);
                            //DbHelper2.ExecuteNonQuery(sql, DbHelper.conStr);

                            U8API u8API = new U8API();
                            String cRe = u8API.InsertInventory(inventory[i], CanShu.conStr);
                            if (cRe == "ok")
                            {
                                InventoryResult result = new InventoryResult();
                                result.Result = "添加成功";
                                result.InvCode = inventory[i].InvCode;
                                lst.Add(result);
                            }
                            else
                            {
                                throw new Exception("添加错误："+ cRe);
                                 
                            }
                        //}

                    }
                    catch (Exception ex)
                    {
                        ierror++;
                        InventoryResult result = new InventoryResult();
                        result.Result = ex.Message;
                        result.InvCode = inventory[i].InvCode;
                        lst.Add(result);
                    }

                }


            if (ierror==0)
            {
                retModel.code = 200;
                retModel.message = "添加成功";
                retModel.data = lst;
            }
            else
            {

                retModel.code = 500;
                retModel.message = "添加错误";
                retModel.data = lst;
            }
          

           
            return retModel;


        }
    }
}
